//审批提醒
$(function () {
    createCurrentTask();
})

function createCurrentTask() {
    var options = {
        url: $.webPath() + "/flow/publish/ownerTask",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true
    };
    var columns = {columns: [
        [
            {field: 'executionId', title: '名称', width: 50, formatter: formatName},
            {field: 'activityName', title: '状态', width: 50},
            {field: 'opera', title: '提交人', width: 70},
            {field: 'time', title: '时间', width: 50},
            {field: 'result', title: '结果', width: 30, formatter: formatPic}
        ]
    ]}
    options = $.extend(options, columns);
    $('#dbrw-grid').datagrid(options);
}


function formatName(val, row) {
    var type = val.substring(0, val.indexOf('.'));
    return '<a href="javascript:viewDetail(\'' + row.id + '\',\'' + val + '\',\'' + type + '\');" style="color: red">' + type + '</a>';
}

function formatPic(val,row){
    var type = row.executionId.substring(0, row.executionId.indexOf('.'));
    return '<a href="javascript:viewPicture(\'' + row.id + '\',\'' + row.executionId + '\',\'' + type + '\');" style="color: red">' + val + '</a>';
}

function viewDetail(taskId, executionId, type) {
    if (type == '协议流程') {
        $.post($.webPath() + "/jhgl/xygl/xyglByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jhgl/xygl/xygl_detail?type=" + obj.type + "||xyId=" + obj.id;
            var url = $.webPath() + "/jhgl/xygl/examineTask";
            popWindow(href, url, type, taskId);
        });
    } else if (type == '采购合同流程') {
        $.post($.webPath() + "/htgl/cghtgl/cghtByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/htgl/cghtgl/cghtgl_detail?type=" + obj.type + "||cghtId=" + obj.id;
            var url = $.webPath() + "/htgl/cghtgl/examineTask";
            popWindow(href, url, type, taskId);
        });
    } else if (type == '销售合同流程') {
        $.post($.webPath() + "/htgl/xshtgl/xshtByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/htgl/xshtgl/xshtgl_detail?type=" + obj.type + "||xshtId=" + obj.id;
            var url = $.webPath() + "/htgl/xshtgl/examineTask";
            popWindow(href, url, type, taskId);
        });
    } else if (type == '自购物资计划流程') {
        $.post($.webPath() + "/jhgl/wzjhgl/wzjhByProcessInstance", {'executionId': executionId, 'type': 'Zgwzjh'}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jhgl/wzjhgl/wzjhglsp_detail?type=" + obj.type + "||id=" + obj.id;
            var url = $.webPath() + "/jhgl/wzjhgl/examineTask?type=" + obj.type;
            popWindow(href, url, type, taskId);
        });
    } else if (type == '自购资金计划流程') {
        $.post($.webPath() + "/jhgl/zjjhgl/zjjhByProcessInstance", {'executionId': executionId, 'type': 'Zgzjjh'}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jhgl/zjjhgl/zjjhgl_detail?type=" + obj.type + "||id=" + obj.id + "||jhsp=true";
            var url = $.webPath() + "/jhgl/zjjhgl/examineTask?type=" + obj.type;
            popWindow(href, url, type, taskId);
        });
    } else if (type == '统配物资计划流程') {
        $.post($.webPath() + "/jhgl/wzjhgl/wzjhByProcessInstance", {'executionId': executionId, 'type': 'Tpwzjh'}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jhgl/wzjhgl/wzjhglsp_detail?type=" + obj.type + "||id=" + obj.id;
            var url = $.webPath() + "/jhgl/wzjhgl/examineTask?type=" + obj.type;
            popWindow(href, url, type, taskId);
        });
    } else if (type == '统配资金计划流程') {
        $.post($.webPath() + "/jhgl/zjjhgl/zjjhByProcessInstance", {'executionId': executionId, 'type': 'Tpzjjh'}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jhgl/zjjhgl/zjjhgl_detail?type=" + obj.type + "||id=" + obj.id;
            var url = $.webPath() + "/jhgl/zjjhgl/examineTask?type=" + obj.type;
            popWindow(href, url, type, taskId);
        });
    } else if (type == '电子借款单流程') {
        $.post($.webPath() + "/jhgl/jkd/jkdByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jhgl/jkd/jkd_detail?type=" + obj.type + "||jkdId=" + obj.id;
            var url = $.webPath() + "/jhgl/jkd/examineTask";
            popWindow(href, url, type, taskId);
        });
    } else if (type == '路径流程') {
        $.post($.webPath() + "/organ/ljwh/ljByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/organ/ljwh/ljwh_detail?type=" + obj.type + "||ljId=" + obj.id;
            var url = $.webPath() + "/organ/ljwh/examineTask";
            popWindow(href, url, type, taskId);
        });
    }else if (type == '企业机构流程') {
        $.post($.webPath() + "/organ/qyjg/qyjgByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/organ/qyjg/qyjg_detail?operation=update||qyjgId=" + obj.id;
            var url = $.webPath() + "/organ/qyjg/examineTask";
            popWindow(href, url, type, taskId);
        });
    }else if (type == '供应商流程') {
        $.post($.webPath() + "/organ/gys/ljLsByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/organ/gys/gys_detail?operation=update||ljLsId=" + obj.id;
            var url = $.webPath() + "/organ/gys/examineTask";
            popWindow(href, url, type, taskId);
        });
    }else if (type == '付款流程') {
        $.post($.webPath() + "/sfgl/fksh/fkshByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/sfgl/fksh/fksh_detail?fkshId=" + obj.id;
            var url = $.webPath() + "/sfgl/fksh/examineTask";
            popWindow(href, url, type, taskId);
        });
    } else if (type == '特批付款流程') {
        $.post($.webPath() + "/sfgl/tpfk/tpfkshByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/sfgl/tpfk/tpfk_detail?tpfkId=" + obj.id;
            var url = $.webPath() + "/sfgl/tpfk/examineTask";
            popWindow(href, url, type, taskId);
        });
    }else if (type == '资金补差单流程') {
        $.post($.webPath() + "/jsgl/zjbcd/zjbcdByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            var href = $.webPath() + "/jsgl/zjbcd/zjbcd_detail?zjbcdId=" + obj.id;
            var url = $.webPath() + "/jsgl/zjbcd/examineTask";
            popWindow(href, url, type, taskId);
        });
    }

}

function popWindow(href, url, type, taskId) {
    var detailwin = $.window({
        iconCls: 'icon-add2',
        title: type,
        url: $.webPath() + '/flow/publish/taskDetail?url=' + href,
        width: 1024,
        height: 500,
        modal: true,
        isMax: false,
        buttons: [
            {text: '审核', handler: function () {

                if (type == '统配物资计划流程') {
                    var hdslJson = "[";
                    $('#wzjhglsp_tabs #wzjhglsp_wzjhmx_grid').edatagrid("saveRow");
                    var rows = $("#wzjhglsp_tabs #wzjhglsp_wzjhmx_grid").edatagrid("getData").rows;
                    for (var i = 0; i < rows.length; i++) {
                        if (!rows[i].hdsl)
                            rows[i].hdsl = 0.00;
                        hdslJson += "{'id':'" + rows[i].id + "','hdsl':" + rows[i].hdsl + "},"
                    }
                    if (rows.length > 0)
                        hdslJson = hdslJson.substring(0, hdslJson.length - 1);
                    hdslJson += "]";

                    var param = {'result': '审核', 'taskId': taskId, 'content': $("#content").text(), 'wzjhmxList': hdslJson};

                    $.post(url, param, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#dbrw-grid').datagrid('reload');
                            $(detailwin).window("close");
                        }
                    })
                } else {
                    $.post(url, {'result': '审核', 'taskId': taskId, 'content': $("#content").text()}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#dbrw-grid').datagrid('reload');
                            $(detailwin).window("close");
                        }
                    })
                }
            }},
            {text: '驳回', handler: function () {
                $.post(url, {'result': '驳回', 'taskId': taskId, 'content': $("#content").text()}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200') {
                        $('#dbrw-grid').datagrid('reload');
                        $(detailwin).window("close");
                    }
                })
            }}
        ]
    });
}

function viewPicture(taskId,executionId,type){
    if (type == '协议流程') {
        $.post($.webPath() + "/jhgl/xygl/xyglByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '采购合同流程') {
        $.post($.webPath() + "/htgl/cghtgl/cghtByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '销售合同流程') {
        $.post($.webPath() + "/htgl/xshtgl/xshtByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '自购物资计划流程') {
        $.post($.webPath() + "/jhgl/wzjhgl/wzjhByProcessInstance", {'executionId': executionId, 'type': 'Zgwzjh'}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '自购资金计划流程') {
        $.post($.webPath() + "/jhgl/zjjhgl/zjjhByProcessInstance", {'executionId': executionId, 'type': 'Zgzjjh'}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '统配物资计划流程') {
        $.post($.webPath() + "/jhgl/wzjhgl/wzjhByProcessInstance", {'executionId': executionId, 'type': 'Tpwzjh'}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '统配资金计划流程') {
        $.post($.webPath() + "/jhgl/zjjhgl/zjjhByProcessInstance", {'executionId': executionId, 'type': 'Tpzjjh'}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '电子借款单流程') {
        $.post($.webPath() + "/jhgl/jkd/jkdByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '路径流程') {
        $.post($.webPath() + "/organ/ljwh/ljByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '企业机构流程') {
        $.post($.webPath() + "/organ/qyjg/qyjgByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    }else if (type == '供应商流程') {
        $.post($.webPath() + "/organ/gys/ljLsByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '付款流程') {
        $.post($.webPath() + "/sfgl/fksh/fkshByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    } else if (type == '特批付款流程') {
        $.post($.webPath() + "/sfgl/tpfk/tpfkshByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    }else if (type == '资金补差单流程') {
        $.post($.webPath() + "/jsgl/zjbcd/zjbcdByProcessInstance", {'executionId': executionId}, function (data) {
            var obj = eval('(' + data + ')');
            $.window({
                title: "流程历史",
                iconCls: 'icon-add2',
                url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(obj.processInstanceId))+ "&businessNo=" + obj.businessNo,
                width: 650,
                height: 500,
                closed: true,
                modal: true,
                isMax: true
            });
        });
    }


}
